﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class CConvertor
{
    float m_fRawFreq;
    List<float> m_ConvertedValList;
    int m_nFreq;

    public CConvertor(float fRawFreq, int nFreq)
    {
        m_fRawFreq = fRawFreq;
        m_nFreq = nFreq;
        m_ConvertedValList = new List<float>();
    }

    public List<float> ConvertRawValues(List<float> RawValuesList)
    {
        // Сначала уберем "неизвестные" значения, которые обозначаются "2200000000"
        for (int i = 0; i < RawValuesList.Count; ++i)
        {
            if (Math.Abs(RawValuesList[i] - 2200000000) < float.Epsilon)
            {
                int k = 0;

                while (Math.Abs(RawValuesList[i + k] - 2200000000) < float.Epsilon)
                {
                    k++;
                    if ((i + k) == RawValuesList.Count)
                    {
                        break;
                    }
                }

                if (i == 0)
                {
                    RawValuesList[i] = RawValuesList[i + k];
                }
                else if ((i + k) == RawValuesList.Count)
                {
                    RawValuesList[i] = RawValuesList[i - 1];
                }
                else
                {
                    RawValuesList[i] = (RawValuesList[i + k] + k * RawValuesList[i - 1]) / (k + 1);
                }
            }
        }

        // Возможно, такая аппроксимация лучше? Она, точно, линейная :)
        int nCount, delta;
        
        if (m_fRawFreq < 1)
        {
            nCount = (int)(m_nFreq / m_fRawFreq);
            delta = 1;
        }
        else
        {
            nCount = m_nFreq;
            delta = (int)m_fRawFreq;
        }
        
        for (int i = 0; i <= (RawValuesList.Count - delta); i += delta)
        {
            for (int h = 0; h < nCount; ++h)
            {
                int j = (int)Math.Floor(((float)h / nCount) * m_fRawFreq);

                float a = RawValuesList[j + i], b;
                if (j + i + 1 < RawValuesList.Count)
                {
                    b = (RawValuesList[j + i + 1] - RawValuesList[j + i]) / ((float)1 / m_fRawFreq);
                }
                else
                {
                    b = (RawValuesList[j + i] - RawValuesList[j + i - 1]) / ((float)1 / m_fRawFreq);
                }

                m_ConvertedValList.Add(a + b * ((float)h / m_nFreq - (float)j / m_fRawFreq));
            }
        }

        return m_ConvertedValList;
    }

}

